package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.TargetsOfImplphaseData;
import com.sinosoft.ptas.entity.TargetsOfImplPhaseEntity;
import com.sinosoft.ptas.service.TargetsOfImplPhaseService;
import com.sinosoft.ptas.system.commom.ExceptionConstant;

/**
 * 实施期目标控制层
 * @author lianWuJun
 * @time   2017年2月20日 上午10:48:27
 */
@Controller
@RequestMapping(value="targetsOfImplPhase")
public class TargetsOfImplPhaseController {
	
	@Autowired
	TargetsOfImplPhaseService targetsOfImplPhaseService;
	
	/**
	 * 创建实施期目标
	 * 传过来的实体类没有编号，要自己在service中生成
	 * @param targetsOfImplPhaseData  
	 * @return 实施期目标传输类
	 */
	@RequestMapping(value="/createTargetsOfImplPhase.service",method=RequestMethod.POST)
	@ResponseBody
	public  ResponseData createTargetsOfImplPhase (@RequestBody TargetsOfImplphaseData targetsOfImplPhaseData){
		if(targetsOfImplPhaseData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			TargetsOfImplPhaseEntity targetsOfImplPhaseEntity=targetsOfImplPhaseData.toEntity();
			String id=targetsOfImplPhaseService.createTargetsOfImplPhase(targetsOfImplPhaseEntity);//返回值
			List<TargetsOfImplPhaseEntity> retrieveTargs = targetsOfImplPhaseService.retrieveTargetsPhaseByReportId(targetsOfImplPhaseEntity.getReportId());
			targetsOfImplPhaseData.setId(id);
			return new ResponseData(true,retrieveTargs);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	/**
	 * 根据实施期目标编号查询实施期目标
	 * @param id
	 * @return 实施期目标传输类
	 */
	@RequestMapping(value="/retrieveTargetsOfImplPhaseById.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetsOfImplPhaseById (String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,"空");
		}
		TargetsOfImplPhaseEntity targetsOfImplPhasEntity=targetsOfImplPhaseService.retrieveTargetsOfImplPhaseById(id);
		TargetsOfImplphaseData targetsOfImplPhaseData=toTargetsOfImplphaseData(targetsOfImplPhasEntity);
		return new ResponseData(true,targetsOfImplPhaseData);
	}
	
	/**
	 * 更新实施期目标
	 * @param targetsOfImplPhaseData
	 * @return 实施期目标传输类
	 */
	@RequestMapping(value="/updateTargetsOfImplPhase.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTargetsOfImplPhase(@RequestBody TargetsOfImplphaseData targetsOfImplPhaseData){
		if(targetsOfImplPhaseData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			TargetsOfImplPhaseEntity targetsOfImplPhaseEntity=targetsOfImplPhaseData.toEntity();
			targetsOfImplPhaseService.updateTargetsOfImplPhase(targetsOfImplPhaseEntity);
			List<TargetsOfImplPhaseEntity> targetsOfImplPhaseList = targetsOfImplPhaseService.retrieveTargetsPhaseByReportId(targetsOfImplPhaseEntity.getReportId());
			return new ResponseData(true,targetsOfImplPhaseList);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	/**
	 * 根据实施期目标编号删除实施期目标
	 * 再返回这个实施期目标所属的申报表下的所以实施期目标信息
	 * @param id reportId
	 * @return 实施期目标传输类集合
	 */
	@RequestMapping(value="/deleteTargetsOfImplPhaseById.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteTargetsOfImplPhaseById(String id,String reportId){
		if(StringUtil.isBlank(id)||StringUtil.isBlank(reportId)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			TargetsOfImplPhaseEntity targetsOfImplPhaseEntity=targetsOfImplPhaseService.retrieveTargetsOfImplPhaseById(id);
			if(targetsOfImplPhaseEntity.getReportId().equals(reportId)){
				targetsOfImplPhaseService.deleteTargetsOfImplPhaseById(id);
				List<TargetsOfImplPhaseEntity> TargetsOfImplPhaseEntitys=targetsOfImplPhaseService.retrieveTargetsPhaseByReportId(reportId);
				List<TargetsOfImplphaseData> TargetsOfImplphaseDatas=getTargetsOfImplphaseDatas(TargetsOfImplPhaseEntitys);
				return new ResponseData(true,TargetsOfImplphaseDatas);
			}else{
				return new ResponseData(false,"传入的申报表编号与当前实施期目标所在的申报表的编号不匹配");
			}
			
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	/**
	 * 根据组织机构id和行政区划编码查询出司局下面所挂省的实施期目标
	 * @param devisionCode 行政区划代码
	 * @param oid 组织机构代码
	 */
	@RequestMapping(value="/retrieveTargetsPhaseByDivisionCodeAndOrgId.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetsPhaseByDivisionCodeAndOrgId(String divisionCode,String oid){
		if(StringUtil.isBlank(divisionCode)||StringUtil.isBlank(oid)){
			return new ResponseData(false,"传入参数为空");
		}
		List<TargetsOfImplPhaseEntity> targets = targetsOfImplPhaseService.retrieveTargetsPhaseByDivisionCodeAndOrgId(divisionCode, oid);
		return new ResponseData(true,targets);
	}
	
	
	/**
	 * 实施期目标实体类转化为传输类
	 * @param targetsOfImplPhaseEntitys
	 * @return 实施期目标传输类
	 */
	private List<TargetsOfImplphaseData> getTargetsOfImplphaseDatas(List<TargetsOfImplPhaseEntity> targetsOfImplPhaseEntitys) {
		int size = targetsOfImplPhaseEntitys.size();
		List<TargetsOfImplphaseData> targetsOfImplphaseDatas = new ArrayList<TargetsOfImplphaseData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			TargetsOfImplphaseData targetsOfImplphaseData = new TargetsOfImplphaseData();
			targetsOfImplphaseData.totargetsOfImplPhaseData(targetsOfImplPhaseEntitys.get(i));
			targetsOfImplphaseDatas.add(targetsOfImplphaseData);
		}
		return targetsOfImplphaseDatas;
	}

	/**
	 * 实施期目标实体类转化为传输类
	 * @param targetsOfImplPhaseEntity
	 * @return 实施期目标传输类
	 */
	private TargetsOfImplphaseData toTargetsOfImplphaseData(TargetsOfImplPhaseEntity targetsOfImplPhaseEntity) {
		TargetsOfImplphaseData targetsOfImplphaseData = new TargetsOfImplphaseData();
		targetsOfImplphaseData.totargetsOfImplPhaseData(targetsOfImplPhaseEntity);
		return targetsOfImplphaseData;
	}
	
}
